package Java_网络编程.uploadfile;

import java.io.*;
import java.net.Socket;

//客户端上传图片到服务端
class FileClient {
    public static final String Src=new String("C:\\Users\\zl\\Desktop\\A\\test.jpg") ;

    public static void main(String[] args) throws Exception {
        //建立socket通信管道
        Socket socket = new Socket("127.0.0.1",8001);
        OutputStream os = socket.getOutputStream();
        BufferedOutputStream bos = new BufferedOutputStream(os);   //包装成字节缓冲输出流
        //PrintStream ps = new PrintStream(os);//包装成打印流

        //把文件传入I/O流
        InputStream fileSrc = new FileInputStream(Src);
        BufferedInputStream bsc = new BufferedInputStream(fileSrc);
        byte[] buffer=new byte[1024];
        int len;
        while ((len=bsc.read(buffer))!=-1)
        {
            bos.write(buffer,0,len);

        }
        bos.flush();
        bsc.close();
        System.out.println("文件上传完成");
        socket.shutdownOutput();//客户端结束输出,通知服务器数据已经发送完成
        //接收服务端发回来的响应消息
         BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); //缓冲字符输入流
        System.out.println(br.readLine());

    }
}